import wx
from log_object import *

def makeNew(parent, window, param1):
    """ this is how we instantiate the device object from
        the main program that loads this plugin """
    return BusyGraph(parent, window, param1)

class BusyGraph:
	def __init__(self, parent, window, param1):
		self.parent = parent
		self.window = window

		self.type = BUSYGRAPH 
		self.param1 = param1
		self.param2 = NOT_DEFINED 

	def draw(self, dc, start, end, dim, space):
		active_gap =  self.parent.activeTime
		log_date = ""

		foreground = self.parent.getForegroundColor()
		background = self.parent.getBackgroundColor()

		dc.SetBrush(wx.TRANSPARENT_BRUSH)
		dc.SetPen(wx.Pen(foreground, 1))
		dc.SetTextBackground(background)
		dc.SetTextForeground(foreground)
		font = wx.Font(15, wx.TELETYPE, wx.NORMAL, wx.BOLD)
		dc.SetFont(font)

		base_unit = 10
		window_width = int(self.window.Width / space[0])
		window_height = int(self.window.Height / space[1])

		# 1. draw vertical line & information
		x = (dim[0] * window_width) + 30
		origin_x = x
		y = (dim[1] * window_height) + window_height -61
		dc.DrawLines(((x, dim[1] * window_height), (x, y)))
		right_gap = 50
		graph_width = window_width - right_gap 
		endx = (dim[0] * window_width) + graph_width 
		dc.DrawText("time (unit: min.) ->", window_width - 190, y+30)

		color=self.parent.color_mgr.getColor(18)
		dc.SetBrush(wx.Brush(color))
		dc.SetPen(wx.Pen(color))
		dc.DrawRectangle(x + 70, y+30, 15, 15)
		dc.DrawText("active:", x, y+30)
		color=self.parent.color_mgr.getColor(13)
		dc.SetBrush(wx.Brush(color))
		dc.SetPen(wx.Pen(color))
		dc.DrawRectangle(x + 190, y+30, 15, 15)
		dc.DrawText("inactive:", x+100, y+30)

		dc.SetBrush(wx.TRANSPARENT_BRUSH)
		dc.SetPen(wx.Pen(wx.Colour(250, 128, 114), 1))
		dc.DrawLines(((x + 290, y + 30), (x+290, y+50)))
		dc.DrawLines(((x+285, y + 30), (x+295, y+30)))
		dc.DrawText("enter:", x+220, y+30)

		dc.DrawLines(((x + 400, y + 30), (x+400, y+50)))
		dc.DrawLines(((x+395, y + 50), (x+405, y+50)))
		dc.DrawText("leave:", x+330, y+30)
 		y_start = dim[1] * window_height 

		# 2. define & draw area for each participants
		dc.SetPen(wx.Pen(foreground, 1))
		for current_log in self.parent.log_list :
			if end == -1 or end > current_log.duration:
				end = current_log.duration
			log_date = current_log.date

			# adjust base_unit
			#base_unit = int((end - start) / 5.0)
			#tmp = (end - start) % 5
			#if tmp > 1 : 
			#	base_unit  += 1

			base_unit = 10
			
			no_user = len(self.parent.selected_user_list)
			y = y_start + window_height -61
			each_height = window_height -61 
			if no_user > 0 : 
				each_height = (window_height - 61) / no_user 
				y = y_start  
			else :
				dc.DrawLines(((x, y), (endx+right_gap, y)))
			for pointer in current_log.pointer_list: 
				if self.parent.isSelectedUser(pointer.name) == False:
					continue
				y += each_height 
				dc.DrawLines(((x, y), (endx+right_gap, y)))
				dc.DrawLines(((x-5, y - each_height + 50), (endx+right_gap, y - each_height + 50)))
				dc.DrawText(pointer.name, x+ 20, y - each_height + 10)

			# 2.1 draw ruler for x and y 
			# get white color
			taglist = self.parent.getTagList()
			if len(taglist) > 0 : 
				color=self.parent.color_mgr.getColor(1)
				dc.SetTextForeground(color)
			font = wx.Font(12, wx.TELETYPE, wx.NORMAL, wx.BOLD)
			dc.SetFont(font)
			dc.DrawText(str(start), x-7, y+10)
			unit_x = (end - start) / (base_unit *1.0)
			if unit_x < 1 : 
				unit_x =1
			width_x = graph_width / unit_x
			time_unit = start 

			# draw... purpose...
			tmp_width_x = width_x / (base_unit * 1.0) 
			start_x = origin_x
			color_idx = 14
			for t in taglist : 
				tint = int(t)
				x =(tint * tmp_width_x) + origin_x 
				color=self.parent.color_mgr.getColor(color_idx)
				dc.SetBrush(wx.Brush(color))
				dc.SetPen(wx.Pen(color, 1))
				#dc.DrawLines(((x, y), (x, y+each_height+50)))
				dc.DrawRectangle(start_x, y_start+each_height+1, x - start_x, 25)
				#dc.DrawRectangle(start_x, 24, x - start_x, 25)
				start_x = x
				color_idx += 1

			dc.SetPen(wx.Pen(foreground, 1, style=wx.DOT))
			if len(taglist) > 0 : 
				color=self.parent.color_mgr.getColor(1)
				dc.SetPen(wx.Pen(color, 1, style=wx.DOT))

			del taglist 

			unit_x = int(unit_x) 
			x = origin_x
			for i in range(unit_x):
				time_unit += base_unit 
				if time_unit > end :
					tmp = base_unit - time_unit + end 
					x +=  (width_x * tmp / base_unit)
					dc.DrawLines(((x, 50), (x, y+5)))
					dc.DrawText(str(end), x-7, y+10)
					break
				x += width_x
				dc.DrawLines(((x,  y_start + 50), (x, y+25)))
				dc.DrawText(str(time_unit), x-7, y+10)

			
			# draw... points...
			dc.SetTextForeground(foreground)
			# 2.2 draw active / inactive area graph
			width_x = width_x / (base_unit * 1.0) 
			y = y_start
			for pointer in current_log.pointer_list: 
				if self.parent.isSelectedUser(pointer.name) == False:
					continue

				active_period = 0
				inactive_period = 0
				for active_slot in pointer.active_range:
					# 0/1, start, end
					active_start = active_slot[1] / 60
					active_end = active_slot[2] / 60

					# adjust start/end
					if active_start < start:
						if active_end < start:
							continue
						else :
							active_start = start 
					if active_end > end:
						active_end = end

					# setting color for status
					if active_slot[0] == 0 :
						color=self.parent.color_mgr.getColor(13)
						dc.SetBrush(wx.Brush(color))
						dc.SetPen(wx.Pen(color, 1))
						# hj
						#active_period += (active_end - active_start) 
						inactive_period += (active_end - active_start) 
					else :
						color=self.parent.color_mgr.getColor(18)
						dc.SetBrush(wx.Brush(color))
						dc.SetPen(wx.Pen(color, 1))
						active_period += (active_end - active_start) 
						#print active_start, active_end

					# calculate position in window
					start_x =((active_start - start) * width_x) + origin_x 
					x =((active_end - start) * width_x) + origin_x 
					width = int(x - start_x)
					if width == 0 :
						dc.DrawLines(((start_x, y+50), (start_x, y+each_height)))
					else:
						dc.DrawRectangle(start_x, y+50, x - start_x, each_height-50)

				# draw in and out
				for log in pointer.log_list : 
					# for testing, draw in/out
					if log.mode == LOG_POINTER_NEW :
						log_min = log.time / 60
						x =((log_min - start) * width_x) + origin_x 
						dc.SetPen(wx.Pen(wx.Colour(250, 128, 114), 1))
						dc.DrawLines(((x, y + 30), (x, y+50)))
						dc.DrawLines(((x-5, y + 30), (x+5, y+30)))
					elif log.mode == LOG_POINTER_REMOVE :
						log_min = log.time / 60
						x =((log_min - start) * width_x) + origin_x 
						line_y = y + 50 
						dc.SetPen(wx.Pen(wx.Colour(250, 128, 114), 1))
						dc.DrawLines(((x, y + 30), (x, line_y)))
						dc.DrawLines(((x-5, line_y-2), (x+5, line_y-2)))
				#print "active_period = ", active_period
				#print active_period, inactive_period
				active_period = int((active_period * 1.0 / end) * 100)
				inactive_period = int((inactive_period * 1.0 / end) * 100)
				dc.DrawText("active: " + str(active_period) + "%", origin_x + 100, y + 10)
				dc.DrawText("inactive: " + str(inactive_period) + "%", origin_x + 250, y + 10)
				if active_period != 0.0 : 
					inactive_period = int((inactive_period * 1.0 / active_period) * 100)
					dc.DrawText("(inactive/active=" + str(inactive_period) + "%)", origin_x + 350, y + 10)
				#dc.DrawText("clicks: " + str(pointer.no_clicks) + ", dclick: " + str(pointer.no_dclicks), origin_x + 550, y + 10)

				y += each_height 
					

		font = wx.Font(12, wx.TELETYPE, wx.NORMAL, wx.BOLD)
		dc.SetFont(font)
		x = (dim[0] * window_width) + window_width -100
		y = (dim[1] * window_height) + 10
		dc.DrawText(log_date, x, y)


